module.exports = async (params) => {

    let comDriver = require('@/common/driver');
    let request = await comDriver.app().core.getBeanClass('server.Request');

    let BaseForm = await comDriver.app().core.getBeanClass('library.BaseForm');
    let TextEdit = await comDriver.app().core.getBeanClass('library.TextEdit');
    let CheckBox = await comDriver.app().core.getBeanClass('library.CheckBox');
    let Button = await comDriver.app().core.getBeanClass('library.Button');

    class Bean extends BaseForm {
        loadControls() {
            super.loadControls();

            this.addControl(new TextEdit({id: 'edtUserName', label: '用户名', field: 'name', text: '', autofocus: true, checkers: [{name: 'checker.control.text.empty', }, {name: 'checker.control.text.min.length', value: 5, },]}));
            this.addControl(new TextEdit({id: 'edtPassword', label: '密码', field: 'password', text: '', checkers: [{name: 'checker.control.text.empty', }, ] }));
            this.addControl(new CheckBox({id: 'chkSaveUser', label: '记住登录状态', field: 'saveuser', value: false, }));
            this.addControl(new Button({id: 'btnLogin', label: '登录', onClick: () => {console.log('btnlogin');}}));
        }

        async queryData(param) {
            let data = await super.queryData(param);
            data.name = comDriver.getCookie('name');
            data.password = comDriver.getCookie('password');
            data.saveuser = comDriver.getCookie('saveuser') || (comDriver.getCookie('saveuser') === null) ? true : false;
            return data;
        }

        async saveData(data) {
            await super.saveData(data);
            let result = await request('login', data);
            comDriver.setCookie('saveuser', data.saveuser ? true : false);
            comDriver.setCookie('name', data.saveuser ? data.name : '');
            comDriver.setCookie('password', data.saveuser ? data.password : '');
            return result;
        }

        async saveAfter(result) {
            await super.saveAfter(result);
            console.log('main.saveAfter', result);
            return await comDriver.app().core.runBean('form.login', {});
        }

    }
    new Bean().run(params);
    return params;
};
